﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;


namespace HdipCricketClub.ViewModels
{
    public class SelectedPlayersViewModel
    {
        // properties

        public Fixture SelectedFixture { get; set; }

        public List<Member> PlayersSelected { get; set; }

        public List<Member> PlayersNotSelected { get; set; }

        public int noOfPlayersSelected { get; set; }

        public int noOfPlayersRequired { get; set; }

        // repositories

        FixtureRepository fixtureRepository = new FixtureRepository();

        // methods

        public SelectedPlayersViewModel ParseFormCollection(FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("SelectedFixture.fixture_id");
            string[] memberId = formCollection.GetValues("item.member_id");
            string[] selected = formCollection.GetValues("item.Selected");

            SelectedPlayersViewModel selectedPlayers = new SelectedPlayersViewModel();

            selectedPlayers.SelectedFixture = fixtureRepository.GetFixture(Int32.Parse(fixtureId[0]));

            List<Member> playersSelected = new List<Member>();
            List<Member> playersNotSelected = new List<Member>();

            int j = 0;

            for (int i = 0; i < memberId.Length; i++)
            {
                Member member = new Member();

                member.member_id = Int32.Parse(memberId[i]);

                if (bool.Parse(selected[j]) == true)
                {
                    member.Selected = true;
                    j += 2;
                    playersSelected.Add(member);
                }
                else if (bool.Parse(selected[j]) == false)
                {
                    member.Selected = false;
                    j += 1;
                    playersNotSelected.Add(member);
                }
            }

            selectedPlayers.PlayersSelected = playersSelected;
            selectedPlayers.PlayersNotSelected = playersNotSelected;
            selectedPlayers.noOfPlayersSelected = selectedPlayers.PlayersSelected.Count();
            selectedPlayers.noOfPlayersRequired = selectedPlayers.SelectedFixture.no_of_players;


            return selectedPlayers;
        }

    }
}